package com.competition.login.controller;


import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.R;
import com.competition.login.entity.Manage;
import com.competition.login.entity.User;
import com.competition.login.service.ManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 管理员登录表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-03
 */
@RestController
@RequestMapping("/login/manage")
@CrossOrigin
@Api(description = "管理员登录相关的接口")
public class ManageLoginController {

    @Autowired
    private ManageService manageService;
    //登录
    @PostMapping("login")
    @ApiOperation("接收管理员输入信息(邮箱和密码)，返回token字符串")
    public R loginUser(@RequestBody Manage manage){
        System.out.println(manage);
        String token= manageService.login(manage);
        return R.ok().data("token",token);
    }

    //根据token获取用户信息
    @GetMapping("info")
    @ApiOperation("登录成功后调用这个接口获取用户数据")
    public R getUserInfo(HttpServletRequest request){
        String memberId= JwtUtils.getMemberIdByJwtToken(request);
        Manage manage = manageService.getById(memberId);
        return R.ok().data("manage", manage);
    }
}

